//
//  HSHomeworkListModel.swift
//  HomeSchoolDemo
//
//  Created by Mac on 16/10/11.
//  Copyright © 2016年 chendong. All rights reserved.
//

import UIKit

//定义闭包类型
//typealias homeworkcallback = (_ err:Error?,_ isRefresh:Bool) -> ()

class HSHomeworkListModel: NSObject {

    var homeworks:[HSHomeworkViewModel] = []
    
    //请求数据
    func requestHomework() {
        let url =  "ZH-schoolname-S-getHomeworkBase"
        let key = userInfo["key"] as! String
        let id = 0
        let param = ["key":key,"id":id] as [String : Any]
        HSNetworkTools.shared.downloadData(url: url, parameters: param) {[weak self] (data, error) in
            print(data,error)
            guard let data = data as? NSDictionary else{
                HSAlertView.bottomAlertView("请检查网络连接")
                //finished(error, false)
                return
            }
            switch data["result"] as! Int {
            case 0:
                //字典转模型
                let dictArray = data["data"] as! [[String:Any]]
                //创建一个保存模型的数组
                var homeworkArray = [HSHomeworkViewModel]()
                //循环
                for dict in dictArray {
                    let homework = HSHomeworkModel(dict: dict)
                    homeworkArray.append(HSHomeworkViewModel(model: homework))
                }
                self?.homeworks = homeworkArray
                //finished(nil, true)
                
            case 1:
                HSAlertView.bottomAlertView("已经没有更早的数据了")
                //finished(nil, false)
            default:
                break
            }
        }
    }
    
}
